package smu.KYBank;

import android.content.ContentValues;
import android.content.Context;
//import android.database.Cursor;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class DBHandler {
	private DBHelper helper;
	private SQLiteDatabase db;

	private DBHandler(Context ctx) {
		this.helper = new DBHelper(ctx);
		this.db = helper.getWritableDatabase();
	}

	public static DBHandler open(Context ctx) throws SQLException {
		DBHandler handler = new DBHandler(ctx);

		return handler;
	}

	public void close() {
		helper.close();
	}

	public long insert(int money) {
		ContentValues values = new ContentValues();
		values.put("member", "nol");
		//values.put("id", 1);
		
		values.put("balance", money);

		return db.insert("account", null, values);
	}
	
	public long io_cash(int account_num,int money) {
		ContentValues values = new ContentValues();
		values.put("balance", money);
		

		return db.update("account", values, "account_num=" + account_num, null);
	}
	
	public Cursor select_acnt(int account_num) throws SQLException {
		Cursor cursor = db.query(true, "account", new String[] { "account_num",
				"member", "balance"}, "account_num" + "=" + account_num, null, null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}
	public Cursor select_member(String name) throws SQLException {
		Cursor cursor = db.query(true, "account", new String[] { "account_num",
				"member", "balance"}, "member" + "='" + name+"'", null, null, null, null, null);
		if (cursor != null) {
			cursor.moveToFirst();
		}
		return cursor;
	}
}

